package com.zoffy.wmsbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zoffy.wmsbackend.common.BaseResponse;
import com.zoffy.wmsbackend.common.CommonObj;
import com.zoffy.wmsbackend.common.ErrorCode;
import com.zoffy.wmsbackend.common.ResultUtils;
import com.zoffy.wmsbackend.exception.BusinessException;
import com.zoffy.wmsbackend.model.dto.GenerateTransferOrderByPlanDTO;
import com.zoffy.wmsbackend.model.dto.transfer.TransferDTO;
import com.zoffy.wmsbackend.model.dto.transfer.TransferOrderDto;
import com.zoffy.wmsbackend.model.vo.transfer.TransferOrderDetailsVo;
import com.zoffy.wmsbackend.model.vo.transfer.TransferOrderVo;
import com.zoffy.wmsbackend.service.TransferOrderService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@PreAuthorize("hasAuthority('admin')")
@RequestMapping("/transfer")
public class TransferOrderController {

    @Resource
    private TransferOrderService transferOrderService;

    @PostMapping("/import")
    public BaseResponse<Boolean> importTransfer(@RequestParam("file") MultipartFile file) throws IOException {
        Boolean res = transferOrderService.importTransfer(file);
        return ResultUtils.success(res);
    }

    @PostMapping("/add")
    public BaseResponse<Boolean> addTransfer(@RequestBody TransferDTO transferDto) {
        Boolean res = transferOrderService.transfer(transferDto);
        return ResultUtils.success(res);
    }

    @PostMapping("/list")
    public BaseResponse<Page<TransferOrderVo>> listByPage(@RequestBody com.zoffy.wmsbackend.common.RequestParam<TransferOrderDto> requestParam) {
        Page<TransferOrderVo> res = transferOrderService.listTransferOrderVoByPage(requestParam);
        return ResultUtils.success(res);
    }

    @PostMapping("/approval")
    public BaseResponse<Boolean> approval(@RequestBody CommonObj<Long> transferId) {
        Long id = transferId.getData();
        boolean res = transferOrderService.approval(id);
        return ResultUtils.success(res);
    }

    @GetMapping("/")
    public BaseResponse<TransferOrderDetailsVo> getDetailsById(@RequestParam("id") Long id) {
        TransferOrderDetailsVo res = transferOrderService.getDetailsById(id);
        return ResultUtils.success(res);
    }

    @PostMapping("/generate-by-plan")
    public BaseResponse<Boolean> generateTransferOrderByPlan(@RequestBody GenerateTransferOrderByPlanDTO dto) {
        Long materialUsagePlanId = dto.getMaterialUsagePlanId();
        Long targetWarehouseId = dto.getTargetWarehouseId();
        Long originWarehouseId = dto.getOriginWarehouseId();
        Boolean result = transferOrderService.generateTransferOrderByPlan(originWarehouseId, targetWarehouseId, materialUsagePlanId);
        return ResultUtils.success(result);
    }
}